import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import "./index.less";
import { drugInfoApi } from "@/api/erp/drug/drugCatalogue";
import { drugDatch } from "@/api/MedicalExamination";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { useRef } from "react";
import { drugFrmLossApi } from "@/api/erp/drug/reportingLosses";
let reload: Function;
let pkId: string;
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "drugStorage.drugStorage";
	const row = useRef<any>({});
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.drug`),
			field: "drugId",
			component: "ApiSelect",
			componentProps: {
				api: drugInfoApi.drugsList,
				labelField: "drugName",
				valueField: "drugId",
				onChange: (value: any) => {
					row.current = {
						...row.current,
						productId: value
					};
					onChangeProduct();
				}
			}
		},
		{
			label: t(`${en_vi}.batchNo`),
			field: "batchNo",
			component: "ApiSelect",
			componentProps: {
				api: drugDatch,
				labelField: "batchNo",
				valueField: "batchNo",
				onChange: (value: any) => {
					row.current = {
						...row.current,
						batchNo: value
					};
					onChangeProduct();
				}
			}
		},
		{
			label: t(`${en_vi}.warehouse`),
			field: "whsId",
			component: "ApiSelect",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList,
				labelField: "name",
				valueField: "whsId",
				onChange: (value: any) => {
					row.current = {
						...row.current,
						whsId: value
					};
					onChangeProduct();
				}
			}
		},
		{
			label: t(`${en_vi}.quantity`),
			field: "count",
			component: "InputNumber"
		}
	];

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, async data => {
		reload = data.reload;
		await resetFields();
		if (data.record) {
			pkId = data.record.pkId;
			setFieldsValue(data.record);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields, updateSchema }] = useForm({
		schemas: formSchema,
		labelWidth: 100,
		baseColProps: {
			span: 24
		},
		initialValues: {
			status: "0"
		}
	});
	const onChangeProduct = async () => {
		const { current } = row;
		console.log(current);
		if (!current.batchNo || !current.productId || !current.whsId) return;
		const res = await drugFrmLossApi.drugBatchGetCount(current);
		updateSchema({ count: res.data });
	};
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = drugFrmLossApi.drugFrmLossCreate;
		if (pkId) {
			api = drugFrmLossApi.drugFrmLossUpdate;
		}
		const res = await api({ ...formValue, pkId });
		if (res) {
			pkId ? message.success(t("kevin_ui.updateSuccess")) : message.success(t("kevin_ui.addedSuccessfully"));
			reload();
			closeDrawer();
			pkId = "";
		}
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<div className="warehouses">
				<BasicForm register={register}></BasicForm>
			</div>
		</BasicDrawer>
	);
};
export default index;
